//
//  YDDayNewsController.m
//  siyanone
//
//  Created by LK-IOS on 2018/3/23.
//  Copyright © 2018年 LK-News. All rights reserved.
//

#import "YDDayNewsController.h"
#import "YDADayNewsModel.h"
#import "YDACalendarView.h"
#import "YDADayNewsCell.h"



@interface YDDayNewsController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)UITableView *tableView;
/**
 新闻数据源
 */
@property (nonatomic, strong)NSMutableArray *newsList;

/**
 当前页码
 */
@property (nonatomic , assign) NSInteger currentPage;

/**
 日历
 */
@property (nonatomic, strong) YDACalendarView *calenderView;



//无数据
@property (nonatomic , weak) SYNoDataView *dataView;
//网络占位图
@property (nonatomic, weak) SYNoNetView *noNetView;

@end

@implementation YDDayNewsController

-(NSMutableArray *)newsList
{
    if (_newsList == nil) {
        _newsList = [NSMutableArray new];
    }
    return _newsList;
}


- (void)removeNoNetView
{
    if (_noNetView != nil) {
        [_noNetView removeFromSuperview];
        _noNetView = nil;
    }
}

- (void)removeNodataView
{
    if (_dataView != nil) {
        [_dataView removeFromSuperview];
        _dataView = nil;
    }
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:mainGray];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.title = @"7*24快讯";
    [self tableView];
    [self loadData];
    [self setUpDataManger];
}

-(void)setUpDataManger
{
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    
    header.lastUpdatedTimeLabel.hidden = NO;//是否隐藏最后一次刷新时间
    header.stateLabel.hidden = NO;//是否隐藏刷新状
    self.tableView.mj_header = header;
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    self.tableView.mj_footer = footer;
    self.tableView.mj_footer.hidden = YES;
    
    
}
- (void)loadData
{
    self.currentPage = 0;
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:@(self.currentPage) forKey:@"page"];
    [dict setObject:@(10) forKey:@"pageSize"];
    
    [BaseNetRequest sessionRequestWithparameters:dict method:GETMethod requstType:AllDayNews WithSuccessBlock:^(NSDictionary *resultDict) {
        
        [self.tableView.mj_header endRefreshing];
        if ([resultDict[@"code"] integerValue] == 200) {
            
            [self.newsList removeAllObjects];
            self.newsList = [YDADayNewsModel mj_objectArrayWithKeyValuesArray:resultDict[@"data"]];
            if (self.newsList.count < 10) {
                self.tableView.mj_footer.hidden = NO;
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                self.tableView.mj_footer.hidden = NO;
            }
            
            [self.tableView reloadData];
            
            [self removeNoNetView];
            
            if (self.newsList.count) {
                [self removeNodataView];
                self.calenderView.model = self.newsList.firstObject;
            }else
            {
                [self dataView];
                
            }
            
//            [self postView];
            
        }
        else
        {
            [[NavTipsView defaultTipsView] showTipsBannerInView:self.view WithType:NavTipsViewTypeMessage text:resultDict[@"msg"] isExistNav:YES];
        }
    } failBlock:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        
        if (!self.newsList.count) {
            //没网如果内存没有数据源加载noNetVIew
            [self removeNodataView];
            [self noNetView];
        }
        DDLog(@"+++++++++")
        [[NavTipsView defaultTipsView] showTipsBannerInView:self.view WithType:NavTipsViewTypeMessage text:@"信息加载出错，请重试~" isExistNav:YES];
    }];
    
}


-(void)loadMoreData
{
    self.currentPage++;
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:@(self.currentPage) forKey:@"page"];
    [dict setObject:@(10) forKey:@"pageSize"];
    [BaseNetRequest sessionRequestWithparameters:dict method:GETMethod requstType:AllDayNews WithSuccessBlock:^(NSDictionary *resultDict) {
        
        [self.tableView.mj_footer endRefreshing];
        if ([resultDict[@"code"] integerValue] == 200) {
            
            NSArray *newsArr =  [YDADayNewsModel mj_objectArrayWithKeyValuesArray:resultDict[@"data"]];
            [self.newsList addObjectsFromArray:newsArr];
            
            if (newsArr.count < 10) {
                self.tableView.mj_footer.hidden = NO;
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                self.tableView.mj_footer.hidden = NO;
                [self.tableView.mj_footer endRefreshing];
            }
            [self.tableView reloadData];
            
            
        }
        else
        {
            [[NavTipsView defaultTipsView] showTipsBannerInView:self.view WithType:NavTipsViewTypeMessage text:resultDict[@"msg"] isExistNav:YES];
            
        }
    } failBlock:^(NSError *error) {
        [self.tableView.mj_footer endRefreshing];
        [[NavTipsView defaultTipsView] showTipsBannerInView:self.view WithType:NavTipsViewTypeMessage text:@"信息加载出错，请重试~" isExistNav:YES];
    }];
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.newsList.count ? 1 : 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.newsList.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YDADayNewsModel *model = self.newsList[indexPath.row];
    
    return model.cellH;
    
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return isPhoneX ? rateX812(32) : rate750(32);
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YDADayNewsCell *cell = [YDADayNewsCell cellWithTbaleview:tableView];
    cell.model = [self.newsList objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YDADayNewsModel *model = [self.newsList objectAtIndex:indexPath.row];
    model.isSelected = !model.isSelected;
    
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationFade];
    
    
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return self.calenderView;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    if (self.newsList.count) {
        YDADayNewsCell *cell = (YDADayNewsCell *)[self.tableView visibleCells].lastObject;
        NSIndexPath *indexpath = [self.tableView indexPathForCell:cell];
        YDADayNewsModel *model = self.newsList[indexpath.row];
        self.calenderView.model = model;
    }
    
    
}
/*******tableView********/
-(UITableView *)tableView
{
    if (_tableView == nil) {
        
//        CGFloat safeTopH = isPhoneX ? 84 : 64;
        CGFloat safeBottomH = isPhoneX ? 83 : 49;
        
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, LKScreenW, LKScreenH - safeBottomH) style:UITableViewStylePlain];
        [self.view addSubview:tableView];
        tableView.backgroundColor = [UIColor colorWithHexString:mainGray];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.showsVerticalScrollIndicator = NO;
        tableView.estimatedRowHeight = 0;
        tableView.estimatedSectionFooterHeight = 0;
        tableView.estimatedSectionHeaderHeight = 0;
        tableView.tableFooterView = [UIView new];
        if (isIos11) {
            tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _tableView = tableView;
    }
    return _tableView;
}

-(YDACalendarView *)calenderView
{
    if (_calenderView == nil) {
        _calenderView = [[YDACalendarView alloc] initWithFrame:CGRectMake(0, 0, LKScreenW, isPhoneX ? rateX812(32) : rate750(32))];
    }
    return _calenderView;
}


/*******dataView********/
-(SYNoDataView *)dataView
{
    if (_dataView == nil) {
        CGFloat dataW = rate375(240);
        CGFloat dataX = (LKScreenW - dataW) * 0.5;
        CGFloat dataH = isPhoneX ? rateX812(196) : rate750(196);
        CGFloat dataY = isPhoneX ? rateX812(132) : rate750(132);
        CGRect frame = CGRectMake(dataX, dataY, dataW, dataH);
        //wuneirong   wuxinhao
        SYNoDataView *dataView = [SYNoDataView positionNoDataViewInitWithFrame:frame ImageString:@"wuneirong" buttonTitle:@"" IsCanSeeBuyButton:NO];
        [self.tableView addSubview:dataView];
        _dataView = dataView;
    }
    return _dataView;
}

-(SYNoNetView *)noNetView
{
    if (_noNetView == nil) {
        SYNoNetView *noNetView = [[SYNoNetView alloc] init];
        [self.tableView addSubview:noNetView];
        
        [noNetView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.view);
            make.top.equalTo(isPhoneX ? @(rateX812(40)) : @(rate750(40)));
        }];
        LK(weself)
        [noNetView setClickBlock:^(BOOL isClick) {
            [weself.tableView.mj_header beginRefreshing];
            
        }];
        _noNetView = noNetView;
    }
    return _noNetView;
}

-(void)postView
{
    LK(weakSelf)
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        PZTipsAlertView *alert = [PZTipsViewManger shareSingleTipsAlertViewTitle:@"提示" Mesage:@"点击内容可展开" ButtonTitle:@"我知道了" ButtonClickBlock:^{
            [weakSelf.pz_popupController dismiss];
        }];
        
        weakSelf.pz_popupController = [[PZPopUpController alloc] init];
        weakSelf.pz_popupController.layoutType = PZPopupLayoutTypeCenter;
        weakSelf.pz_popupController.slideStyle = PZPopupSlideStyleFade;
        [weakSelf.pz_popupController presentContentView:alert duration:0.75 springAnimated:YES];
        
    });
    
}

@end
